package com.zhaoyan.importer;

import static org.junit.Assert.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.bot.utils.FileUtils;
import com.zhaoyan.entity.Goods;
import com.zhaoyan.reader.ReaderUtils;

/**
 * @author zhaoyan
 *
 *	2020年3月2日
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-context.xml")
public class ImportData {

	
	@Resource
	private RedisTemplate redisTemplate;
	
	//	导入数据
	@Test
	public void test() throws IOException {
		
		
		String file = FileUtils.class.getClassLoader().getResource("goods.txt").getFile();
		
		List<String> readerUtils = ReaderUtils.readerUtils(file);
		
		BoundListOperations opsList = redisTemplate.boundListOps("goods_list");
		
		//1==微星GP75轻薄17英寸游戏本==¥8999==20%
		
		for (String goodStr : readerUtils) {
			
			String[] split = goodStr.split("==");
			
			Goods realGoods = new Goods();
			
			realGoods.setId(Integer.parseInt(split[0]));
			//	判断
			if(ReaderUtils.hasText(split[1]))
			realGoods.setName(split[1]);
			
			//	判断
			if(ReaderUtils.isNumber(split[2])) {
				String price = split[2].substring(1);
				realGoods.setPrice( new BigDecimal(Double.parseDouble(price)));
			}
			if(split.length > 2) {
				
				realGoods.setSelPer(split[3]);
			}else {
				realGoods.setSelPer("0%");
				
			}
			
			opsList.leftPush(realGoods);
			
		}
			
	}
	
	
	
	
	

}
